package com.orange.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.dbhelp.page.Page;
import com.orange.handler.AuthSchoolContactService;
import com.orange.modules.bean.SchoolContactsBean;
import com.orange.modules.bean.SessionUser;
import com.orange.modules.entity.school.AuthSchoolContacts;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/contacts")
public class AuthSchoolContactsController extends BaseController{

	private static final Logger log = Logger.getLogger(AuthSchoolContactsController.class);

	@Autowired
	private AuthSchoolContactService authSchoolContactService;
	
	/** 
	 * 查询联系人信息
	 */
	@RequestMapping(value = "/queryAuthSchoolContacts" ,produces = MediaType.TEXT_HTML_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolContacts(HttpServletRequest request,HttpServletResponse response,
			@RequestParam String pid) throws SQLException{
		try{
			String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(null==schoolId){
				return toResponse(null, ReturnCode.FAIL, "未登录");
			}
			AuthSchoolContacts aa = authSchoolContactService.queryAuthSchoolContactsBypid(pid);
			return toResponse(aa,ReturnCode.SUCCESS,"查询成功");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.FAIL,"暂无数据");
		}
	}
	
	/**
	 * 删除指定的联系人
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/deleteAuthSchoolContacts" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> deleteAuthSchoolContacts(HttpServletRequest request,@RequestBody Map<String,String> map) throws SQLException{
		try{
			String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(schoolId==null || StringUtils.isBlank(schoolId))
				return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"未登录");
			authSchoolContactService.deleteAuthSchoolContactsBypid(map.get("pid"));
			return toResponse(null,ReturnCode.SUCCESS,"删除成功");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.FAIL,"删除失败");	
		}
	}

	/**
	 * 分页显示联系人信息
	 * 
	 */
	@RequestMapping(value = "/queryAuthSchoolContactsByPage" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolContactsByPage(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String pageNumber = map.get("pageNumber");
		String pageSize = "10";
		try {
			Page<AuthSchoolContacts> page = authSchoolContactService.queryAuthSchoolContactsByPage(Integer.valueOf(pageNumber) , Integer.valueOf(pageSize), map);
			//List<AuthSchoolContacts> list = page.getData();
			
			return toResponse(page,ReturnCode.SUCCESS,"查询成功");
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return toResponse(null,ReturnCode.FAIL,"查询失败");
		}
	}
	
	/**
	 * 增加指定学校的联系人
	 */
	@RequestMapping(value = "/insertAuthSchoolContacts" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> insertAuthSchoolContacts(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		AuthSchoolContacts authSchoolContacts = new AuthSchoolContacts();
		try{
		String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
		if(schoolId==null || StringUtils.isBlank(schoolId))
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"学校未登录");
		String name = map.get("name");
		String type = map.get("type");
		String mobilePhone = map.get("mobilePhone");
		String telephone = map.get("telephone");
		String email = map.get("email");
		if(name==null || StringUtils.isBlank(name))
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"姓名不能为空");
		if(type==null || StringUtils.isBlank(type))
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"职务不能为空");
		if(mobilePhone==null || StringUtils.isBlank(mobilePhone))
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"手机号码不能为空");
		if(telephone==null || StringUtils.isBlank(telephone))
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"办公电话不能为空");
		if(email==null || StringUtils.isBlank(email))
			return toResponse(null,ReturnCode.PARAMETER_NOT_NULL,"邮箱不能为空");
		authSchoolContacts.setSchoolId(schoolId);
		authSchoolContacts.setName(name);
		authSchoolContacts.setType(type);
		authSchoolContacts.setMobilePhone(mobilePhone);
		authSchoolContacts.setTelephone(telephone);
		authSchoolContacts.setEmail(email);
		authSchoolContacts.setCreateTime(sdf.format(new Date()));
		authSchoolContactService.insertAuthSchoolContacts(authSchoolContacts);
		return toResponse(authSchoolContacts,ReturnCode.SUCCESS,"新增成功");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.SUCCESS,"新增失败");
		}
	}
	
	/**
	 * 修改联系人信息
	 */
	@RequestMapping(value = "/updateAuthSchoolContacts" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateAuthSchoolContacts(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		try{
			String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(schoolId == null){
				return toResponse(schoolId,ReturnCode.FAIL,"无该条数据");
			}
			AuthSchoolContacts aa = authSchoolContactService.queryAuthSchoolContactsBypid(map.get("pid"));
			
			String name = map.get("name");
			String type = map.get("type");
			String mobilePhone = map.get("mobilePhone");
			String telephone = map.get("telephone");
			String email = map.get("email");
			if(name!=null && StringUtils.isNotBlank(name))
				aa.setName(name);
			if(type!=null && StringUtils.isNotBlank(type))
				aa.setType(type);
			if(mobilePhone!=null && StringUtils.isNotBlank(mobilePhone))
				aa.setMobilePhone(mobilePhone);
			if(telephone!=null && StringUtils.isNotBlank(telephone))
				aa.setTelephone(telephone);
			if(email!=null && StringUtils.isNotBlank(email))
				aa.setEmail(email);
			authSchoolContactService.updateAuthSchoolContacts(aa);
			return toResponse(aa,ReturnCode.SUCCESS,"修改成功");
		}catch(Exception e){
			log.error(e);
			return toResponse(null,ReturnCode.FAIL,"修改失败");
		}
	}	
	/*************************************************************************************************************/
	@RequestMapping(value = "/queryAuthSchoolContactsByPage1" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolContactsByPage1(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String pageNumber = map.get("pageNumber");
		String pageSize = "10";
		try{
			Page<SchoolContactsBean> page = authSchoolContactService.queryAuthSchoolContactsByPage1(Integer.valueOf(pageNumber) , Integer.valueOf(pageSize), map);
			return toResponse(page,ReturnCode.SUCCESS,"查询联系人");
		}catch(Exception e){
			log.error("查询联系人失败",e);
			return toResponse(null,ReturnCode.FAIL,"修改失败");
		}
	}
	
	@RequestMapping(value = "/deleteAuthSchoolContacts1" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> deleteAuthSchoolContacts1(HttpServletRequest request,@RequestParam String pid) throws SQLException{
		try{
			if (StringUtils.isBlank(pid)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL,"参数存在空值");
			}
			AuthSchoolContacts as = authSchoolContactService.queryAuthSchoolContactsBypid(pid);
			if (null==as) {
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "改联系人不存在");
			}
			authSchoolContactService.deleteAuthSchoolContactsBypid(pid);
			return toResponse(null,ReturnCode.SUCCESS,"删除联系人失败");
		}catch(Exception e){
			log.info("删除联系人失败", e);
			return toResponse(null,ReturnCode.FAIL,"删除联系人失败");	
		}
	}
	
	/**
	 * 分页显示联系人信息
	 * 
	 */
	@RequestMapping(value = "/queryAuthSchoolContactsByPage2" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthSchoolContactsByPage2(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String pageNumber = map.get("pageNumber");
		String pageSize = "10";
		try {
			String schoolId = (String) request.getSession().getAttribute(SessionUser.SESSION_USER_KEY);
			if(schoolId == null){
				return toResponse(schoolId,ReturnCode.FAIL,"无该条数据");
			}
			String type = map.get("type");
			Page<AuthSchoolContacts> page = authSchoolContactService.queryAuthSchoolContactsByPage2(Integer.valueOf(pageNumber) , Integer.valueOf(pageSize), schoolId, type);
			
			
			return toResponse(page,ReturnCode.SUCCESS,"查询成功");
		} catch (NumberFormatException e) {
			
			e.printStackTrace();
			return toResponse(null,ReturnCode.FAIL,"查询失败");
		}
	}
}
